<?php

// 第三方代付通知回调控制器

namespace app\api\controller;

use app\api\controller\Base;
use app\api\model\PaymentOrderPaid;
use app\api\model\PaymentOrderAlipayPaid;
use think\facade\Response;

class Pnotify extends Base
{

    /**
     * 第三方游易付代付通知
     */
    public function youyifu()
    {
        try {
            $xml = file_get_contents('php://input');
            $data = str_replace('\/', '/', $xml);
            $xml_obj = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
            $xml_json = json_encode($xml_obj, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            $resarray = json_decode($xml_json, true);

            if (isset($resarray['out_trade_no']) && isset($resarray['transaction_id']) && isset($resarray['state'])) {
                $order_id = $resarray['out_trade_no'];             // 平台订单号
                $cash = $resarray['total_fee'];                    // 金额              
                $other_status = $resarray['state'];                // 第三方状态    
                $other_order_id = $resarray['transaction_id'];     // 第三方订单号 
                $notify_param = $resarray;                         // 通知参数 
                switch ($other_status) {
                    case '1':
                        $status = 2;  // 成功
                        break;
                    case '2':
                        $status = 3;  // 失败
                        break;
                    default:
                        return ['code' => 999, 'msg' => '404'];
                }
                // 实例化代付订单模型
                $payment_order_paid_obj = new PaymentOrderPaid();
                $resdata = $payment_order_paid_obj->notifyAbout($order_id, $notify_param, $cash, $status, $other_order_id, 'youyifu', false, false, true);
                if ($resdata == 'success') {
                    return Response::create('success', 'html');
                }
            }
            return ['code' => 999, 'msg' => '404'];
        } catch (\Exception $e) {
            payment_log(120000, input(), $e->getMessage(), false);
            abort(404, '页面不存在');
        }
    }

    /**
     * 第三方付易通代付通知
     */
    public function fuyitong()
    {
        try {
            if ($this->request->has('status') && $this->request->has('cporder')) {
                $order_id = $this->request->param('cporder');              // 平台订单号
                $cash = $this->request->param('amount');                   // 金额              
                $other_status = $this->request->param('status');           // 第三方状态    
                $other_order_id = $this->request->param('cporder');        // 第三方订单号 
                $notify_param = input();                                   // 通知参数 
                switch ($other_status) {
                    case '1':
                        $status = 2;  // 成功
                        break;
                    case '2':
                        $status = 3;  // 失败
                        break;
                    default:
                        return ['code' => 999, 'msg' => '404'];
                }
                // 实例化代付订单模型
                $payment_order_paid_obj = new PaymentOrderPaid();
                $resdata = $payment_order_paid_obj->notifyAbout($order_id, $notify_param, $cash, $status, $other_order_id, 'fuyitong', false, false, true);
                if ($resdata == 'success') {
                    return Response::create('success', 'html');
                }
            }
            return ['code' => 999, 'msg' => '404'];
        } catch (\Exception $e) {
            // payment_log(120000, input(), $e->getMessage(), false);
            abort(404, '页面不存在');
        }
    }

    /**
     * 第三方大栋代付通知
     */
    public function dadong()
    {
        try {
            if ($this->request->has('code')) {
                $resarray = $this->request->param('biz/a');
                if (isset($resarray['orderStatus'])) {
                    $order_id = $resarray['merchantOrderNo'];           // 平台订单号
                    $cash = $resarray['orderAmount'];                   // 金额              
                    $other_status = $resarray['orderStatus'];           // 第三方状态    
                    $other_order_id = $resarray['platformOrderNo'];     // 第三方订单号 
                    $notify_param = input();                            // 通知参数 
                    switch ($other_status) {
                        case 'Success':
                            $status = 2;  // 成功
                            break;
                        case 'Fail':
                            $status = 3;  // 失败
                            break;
                        default:
                            return ['code' => 999, 'msg' => '404'];
                    }
                    // 实例化代付订单模型
                    $payment_order_paid_obj = new PaymentOrderPaid();
                    $resdata = $payment_order_paid_obj->notifyAbout($order_id, $notify_param, $cash, $status, $other_order_id, 'dadong', false, false, true);
                    if ($resdata == 'success') {
                        return Response::create('SUCCESS', 'html');
                    }
                }
            }
            return ['code' => 999, 'msg' => '404'];
        } catch (\Exception $e) {
            // payment_log(120000, input(), $e->getMessage(), false);
            abort(404, '页面不存在');
        }
    }

    /**
     * 第三方杉德代付通知
     */
    public function sandpay()
    {
        try {
            payment_error_log(147111, input());
            payment_error_log(258111, file_get_contents('php://input'));
            return ['code' => 999, 'msg' => '404'];
        } catch (\Exception $e) {
            // payment_log(120000, input(), $e->getMessage(), false);
            abort(404, '页面不存在');
        }
    }

    /**
     * 第三方招商代付通知
     */
    public function zhaoshang()
    {
    }

    /**
     * 第三方银联代付通知
     */
    public function chinapay()
    {
    }

    /**
     * 第三方宇宝代付通知
     */
    public function yubao()
    {
        try {
            if ($this->request->has('status') && $this->request->has('merchant_order_id')) {
                $order_id = $this->request->param('merchant_order_id');   // 平台订单号
                $cash = $this->request->param('cash');                    // 金额              
                $other_status = $this->request->param('status');          // 第三方状态    
                $other_order_id = $this->request->param('order_id');      // 第三方订单号 
                $notify_param = input();                                  // 通知参数 
                switch ($other_status) {
                    case '2':
                        $status = 2;  // 成功
                        break;
                    case '3':
                        $status = 3;  // 失败
                        break;
                    default:
                        return ['code' => 999, 'msg' => '404'];
                }
                // 实例化代付订单模型
                $payment_order_paid_obj = new PaymentOrderPaid();
                $resdata = $payment_order_paid_obj->notifyAbout($order_id, $notify_param, $cash, $status, $other_order_id, 'yubao', false, false, true);
                if ($resdata == 'success') {
                    return Response::create('success', 'html');
                }
            }
            return ['code' => 999, 'msg' => '404'];
        } catch (\Exception $e) {
            // payment_log(120000, input(), $e->getMessage(), false);
            abort(404, '页面不存在');
        }
    }

    /**
     * 第三方连连代付通知
     */
    public function lianlian()
    {
        try {
            $res = file_get_contents('php://input');
            $resarray = json_decode($res, true);
            if (isset($resarray['result_pay'])) {
                $order_id = $resarray['no_order'];                 // 平台订单号
                $cash = $resarray['money_order'];                  // 金额              
                $other_status = $resarray['result_pay'];           // 第三方状态    
                $other_order_id = $resarray['oid_paybill'];        // 第三方订单号 
                $notify_param = $resarray;                         // 通知参数 
                switch ($other_status) {
                    case 'SUCCESS':
                        $status = 2;  // 成功
                        break;
                    case 'FAILURE':
                        $status = 3;  // 失败
                        break;
                    default:
                        return ['code' => 999, 'msg' => '404'];
                }
                // 实例化代付订单模型
                $payment_order_paid_obj = new PaymentOrderPaid();
                $resdata = $payment_order_paid_obj->notifyAbout($order_id, $notify_param, $cash, $status, $other_order_id, 'lianlian', false, false, true);
                if ($resdata == 'success') {
                    $res = '{"ret_code":"0000","ret_msg":"交易成功"}';
                    return Response::create($res, 'html');
                }
            }
            return ['code' => 999, 'msg' => '404'];
        } catch (\Exception $e) {
            payment_log(120000, input(), $e->getMessage(), false);
            abort(404, '页面不存在');
        }
    }

    /**
     * 第三方游易付支付宝代付通知
     */
    public function youyifuzfb()
    {
        try {
            $xml = file_get_contents('php://input');
            $data = str_replace('\/', '/', $xml);
            $xml_obj = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
            $xml_json = json_encode($xml_obj, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            $resarray = json_decode($xml_json, true);

            if (isset($resarray['out_trade_no']) && isset($resarray['transaction_id']) && isset($resarray['state'])) {
                $order_id = $resarray['out_trade_no'];             // 平台订单号
                $cash = $resarray['total_fee'];                    // 金额              
                $other_status = $resarray['state'];                // 第三方状态    
                $other_order_id = $resarray['transaction_id'];     // 第三方订单号 
                $notify_param = $resarray;                         // 通知参数 
                switch ($other_status) {
                    case '1':
                        $status = 2;  // 成功
                        break;
                    case '2':
                        $status = 3;  // 失败
                        break;
                    default:
                        return ['code' => 999, 'msg' => '404'];
                }
                // 实例化代付订单模型
                $payment_order_paid_obj = new PaymentOrderAlipayPaid();
                $resdata = $payment_order_paid_obj->notifyAbout($order_id, $notify_param, $cash, $status, $other_order_id, 'youyifu', false, false, true);
                if ($resdata == 'success') {
                    return Response::create('success', 'html');
                }
            }
            return ['code' => 999, 'msg' => '404'];
        } catch (\Exception $e) {
            payment_log(120000, input(), $e->getMessage(), false);
            abort(404, '页面不存在');
        }
    }
}
